/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author s0789150
 */
public class EventPacket {

    private int eventID;
    private int dataLength;
    private byte[] payload;
    private Map dict;


    public EventPacket(byte[] packet) {
        try {
            eventID = (int)(packet[0] & 0x000000FF);
            dataLength = (int)(packet[1] & 0x000000FF);
            payload = new byte[dataLength];
            System.arraycopy(packet, 2, payload, 0, dataLength);
            dict = Collections.synchronizedMap(new HashMap<String, Object>());

            switch(eventID) {
                case 0:
                case 1:
                case 3:
                    dict.put("posterAddr", getChunk(0, 7));
                    dict.put("posteeAddr", getChunk(8, 15));
                    break;
                case 2:
                    dict.put("posterAddr", getChunk(0, 7));
                    dict.put("posterLED", getChunk(8, 8));
                    break;
                case 4:
                    dict.put("posteeAddr", getChunk(0, 7));
                    dict.put("posteeLED", getChunk(8, 8));
                    break;
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Object getElement(String key) {
        return dict.get(key);
    }

    private byte[] getChunk(int start, int end) {
        int length = end - start + 1;
        byte[] temp = new byte[length];
        try {
          System.arraycopy(payload, start, temp, 0, length);
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
        return temp;

    }

    public static void main(String args[]) {
        byte[] arr1 = {2, 9, 10, 11, 12, 13, 14, 15, 16, 17, 7};
        byte[] arr2 = {0, 16, 10, 11, 12, 13, 14, 15, 16, 17, 100, 101, 102, 103, 104, 105, 106, 107};
        byte[] arr3 = {3, 16, 10, 11, 12, 13, 14, 15, 16, 17, 100, 101, 102, 103, 104, 105, 106, 107};
        byte[] arr4 = {4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1};
        byte[] arr5 = {1, 16, 10, 11, 12, 13, 14, 15, 16, 17, 100, 101, 102, 103, 104, 105, 106, 107};
        EventPacket packet1 = new EventPacket(arr1);
        EventPacket packet2 = new EventPacket(arr2);
        EventPacket packet3 = new EventPacket(arr3);
        EventPacket packet4 = new EventPacket(arr4);
        EventPacket packet5 = new EventPacket(arr5);
        packet1.printTest();
        packet2.printTest();
        packet3.printTest();
        packet4.printTest();
        packet5.printTest();
    }

    public void printTest() {
        System.out.println("Event ID: "+eventID);
        switch(eventID) {
            case 0:
                System.out.println("I ("+byteToHex((byte[])getElement("posterAddr"))+") have met "+
                        byteToHex((byte[])getElement("posteeAddr")));
                break;
            case 1:
                System.out.println("I ("+byteToHex((byte[])getElement("posterAddr"))+") was in range of "+
                        byteToHex((byte[])getElement("posteeAddr")));
                break;
            case 2:
                System.out.println("My ("+byteToHex((byte[])getElement("posterAddr"))+") LED colour is "+
                        byteToColor((byte[])getElement("posterLED")));
                break;
            case 3:
                System.out.println("I ("+byteToHex((byte[])getElement("posterAddr"))+") am now friends with "+
                        byteToHex((byte[])getElement("posteeAddr")));
                break;
            case 4:
                System.out.println("<<Change LED to: "+byteToColor((byte[])getElement("posteeLED"))+" on "+
                        byteToHex((byte[])getElement("posteeAddr"))+">>");
                break;
        }
    }

    public static String byteToHex(byte[] b) {
        String result = "";
        for (int i=0; i < b.length; i++) {
        result +=
              Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        return result;

    }

    public static String byteToColor(byte[] b) {
        int color = b[0] & 0x000000ff;
        switch(color) {
            case 0:
                return "R+G+B";
            case 1:
                return "G+B";
            case 2:
                return "R+G";
            case 3:
                return "G";
            case 4:
                return "R+B";
            case 5:
                return "B";
            case 6:
                return "R";
            case 7:
                return "BLANK!";
            default:
                return "erroneous!";
        }
    }

}
